﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WindowsForms.AOnline.Presenter.Dictionary.Stock;
using WindowsForms.AOnline.View.Dictionary;
using WindowsForms.AOnline.WindowsForm.FormBase;
using WindowsForms.AOnline.WindowsForm.Resources;

namespace WindowsForms.AOnline.WindowsForm.FormDictionary
{
    public partial class FrmXtraStockDetail : FrmXtraBaseCategoryDetail, IStockView  
    {
        /// <summary>
        /// The _stock presenter
        /// </summary>
        private readonly StockPresenter _stockPresenter;
        public FrmXtraStockDetail()
        {
            InitializeComponent();
            _stockPresenter = new StockPresenter(this);
        }

        public long StockId
        {
            get;
            set;
        }

        public string StockCode
        {
            get { return txtStockCode.Text; }
            set { txtStockCode.Text = value; }
        }

        public string StockName
        {
            get { return txtStockName.Text; }
            set { txtStockName.Text = value; }
        }

        public string Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }

        public bool IsActive
        {
            get { return chkActive.Checked; }
            set { chkActive.Checked = value; }
        }

        public long UnitId
        {
            get;
            set;
        }

        /// <summary>
        /// Initializes the data.
        /// </summary>
        protected override void InitData()
        {
            if (KeyValue != null)
                _stockPresenter.Display(KeyValue);
        }

        /// <summary>
        /// Focuses the control.
        /// </summary>
        protected override void InitControls()
        {
            txtStockCode.Focus();
        }

        /// <summary>
        /// Saves the data.
        /// </summary>
        /// <returns></returns>
        protected override string SaveData()
        {
            return _stockPresenter.Save();
        }

        /// <summary>
        /// Valids the data.
        /// </summary>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        protected override bool ValidData()
        {
            if (string.IsNullOrEmpty(StockCode))
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResStockCode"),
                                ResourceHelper.GetResourceValueByName("ResStockCode"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtStockCode.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(StockName))
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResStockName"),
                                ResourceHelper.GetResourceValueByName("ResStockName"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtStockName.Focus();
                return false;
            }

            return true;
        }
    }
}